<?php
namespace BoxSns\Logic;
class NotiLogic extends \LogicController
{

    /**
     *$user_id 被提醒者
     *$content 被提醒内容
     *$url     点击后跳转的url
     */
    public function send($event="", $params=array(),$type="comment")
    {
       
								$user_id=$params['user_id'];
								$this->data=$params;
								$content=$params['content'];
								if($event){
												$tpl=BoxModel("system_msg_template")->where(array('event'=>$event))->find();	
												if($tpl['content']){
														$content=$this->fetch("",stripslashes(htmlspecialchars_decode($tpl['content'])));
												}
								}
								
								$user_ids=array();
							 if (!is_array($user_id)) {
            $user_ids[$user_id] = $user_id;
        }else{
									   $user_ids=$user_id;
								}
								//$user_id_at=$this->At($content);
								//$user_ids = array_merge($user_id_at,$user_ids);
        $obj = BoxModel("SnsNotification");
        if (!empty($user_ids) && $content) {
            foreach ($user_ids as $user_id) {
                $id = $obj->send($user_id, $content);//发送提醒
                /****提醒数字***/
                if ($id) {
                    BoxModel("SnsNotificationCount")->inc($user_id, $type);
                }
            }
        }
        return $id;
    }

    /***********内容提醒的功能***/
    public function At($content)
    {
        preg_match_all('~\@([\w\d\_\-\x7f-\xff]+)(?:[\r\n\t\s ]+|[\xa1\xa1]+|[\xa3\xac]|[\xef\xbc\x8c]|[\,\.\;\[\#])~', $content, $match);
        $member = BoxModel("SystemMember");
        foreach ($match[1] as $one) {
            $r = $member->where("screen_name='$one'")->field("user_id")->find();
            $user_ids[$r['user_id']] = $r['user_id'];
        }
        return is_array($user_ids) ? $user_ids : array();
    }


    /*******提醒功能数字*$param=array('user_id'=>$user_id)***/
    public function get_notification_count($param)
    {
        $where = array(
            'user_id' => $param['user_id'],
        );
        //BoxModel("SnsNotificationCount")->where($where)->save(array('comment'=>234));
        $data = BoxModel("SnsNotificationCount")->where($where)->find();

        if ($data) {
            $data['noti'] = $data['fans'] + $data['comment'];
            return array(
                'error' => 0,
                'data' => $data,
            );
        } else {
            return array(
                'error' => 1,
            );
        }
    }

    /*******提醒列表$param=[limit]**需要登录*/
    public function get($param)
    {
        $limit = $param['limit'] ? $param['limit'] : 10;
        $data = BoxModel("SnsNotification")->getlist($this->user_id, $limit);
        //BoxModel("SnsNotificationCount")->clear($this->user_id, "comment");
        return $data;
    }

    /*****读了一个提醒$param=[id]*****/
    public function read_one($param)
    {
        ignore_user_abort(true);
        $id = $param['id'];
        $r = BoxModel("SnsNotification")->where(array('id' => $id))->save(array('state' => 1));
        return array('error' => 0, 'msg' => $r);
    }

}
  
  